package func;

import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;

import org.junit.Test;
/*
public interface Consumer<T> {
   void accept(T t);
}
 */
public class ConsumerDemo {
	@Test
	public void test1() {
		// 匿名内部类实现
		Consumer<String> c1 = new Consumer<String>() {
			@Override
			public void accept(String t) {
				System.out.println(t);
			}
		};
		c1.accept("hello");
	}
	@Test
	public void test2() {
		// lambda格式
		Consumer<String> c1 = (t) -> System.out.println(t);
		c1.accept("world");
	}
	@Test
	public void test3() {
		List<Integer> list = Arrays.asList(12,31,324,3453,64);
		// forEach就循环自动调用
		list.forEach(new Consumer<Integer>() {
			@Override
			public void accept(Integer t) {
				System.out.println(t);
			}
		});
		list.forEach((t) -> System.out.println(t));
	}
}
